<div class="locations form">
<div style="margin-bottom:10px;">
  <h3><?php __('Edit Location'); ?></h3>
</div>
<?php echo $form->create('Location');?>
	<fieldset>
<?php
        echo $form->input('page', array('type' => 'hidden'));
        echo $form->input('country_id', array('label' => __('Country Name',true)));
        echo $form->input('parent_id', array(
            'label' => __('State/County',true),
            'empty' => __('+ Create new',true),
            'onchange' => 'addNewState()'
        ));
        echo $form->input('name', array('label' => __('Area Name',true)));
        echo $form->input('zip', array('label' => __('Zip/Postal Code',true)));
?>
      <div class="submit">
        <input type="button" value="<?php __('Find in Map'); ?>" onclick="findMap()" />
        <div id="loading-map" class="loading" style="display:none;"><?php __('loading..'); ?></div>
      </div>
<?php
        echo $form->input('latitude', array('label' => __('Latitude',true)));
        echo $form->input('longitude', array('label' => __('Longitude',true)));
        echo $form->input('timezone', array('label' => __('Timezone',true)));
?>
      <div class="submit">
        <input type="submit" value="<?php __('Save Changes'); ?>" />
      </div>
      <div id="map_canvas" style="margin:0 20px; width:735px; height:360px; border:1px solid #CBCFD4;"></div>
	</fieldset>
<?php echo $form->end();?>
</div>
<div class="actions">
	<ul>
		<li><?php echo $html->link(__('Delete', true), array('action'=>'delete', $form->value('Location.id')), null, sprintf(__('Are you sure you want to delete # %s?', true), $form->value('Location.id'))); ?></li>
		<li><?php echo $html->link(__('List Locations', true), array('action'=>'index'));?></li>
		<li><?php echo $html->link(__('List Countries', true), array('controller'=> 'countries', 'action'=>'index')); ?> </li>
		<li><?php echo $html->link(__('New Country', true), array('controller'=> 'countries', 'action'=>'add')); ?> </li>
	</ul>
</div>

<script type="text/javascript">
var marker    = center = null;
var googleMap = new GMap2(document.getElementById('map_canvas'));
googleMap.addControl(new GSmallMapControl());

function findMap () {
    if (!$F('LocationCountryId')) { // can't proceed without country id..
        return false;
    }

    new Ajax.Request('<?php e($this->base); ?>/locations/geocoding', {
        method: 'post',
        parameters: { country:$F('LocationCountryId'), state:$F('LocationParentId'), area:$F('LocationName') },
        onLoading: function() {
            $('LocationEditForm').disable();
            $('map_canvas').setOpacity(0.5);
            $('loading-map').show();
        },
        onComplete: function() {
            $('LocationEditForm').enable();
            $('map_canvas').setOpacity(1);
            new Effect.Fade('loading-map');
        },
        onSuccess: function(transport, json) {
            if (json && json.status == -1) {
                var err = (json.message) ? json.message : 'Fail to Convert address into geographic coordinates';
            } else if (json && json.status == 1) {
                if (json.accuracy && json.latitude && json.longitude) {
                    updateCoordinate(json.latitude, json.longitude);
                    locateMap(json.latitude, json.longitude, json.accuracy);

                    marker.openInfoWindow('Drag to relocate marker.');
                    new Effect.ScrollTo('LocationLatitude');
                }
            }
        }
    });
} // findMap

function locateMap (l1, l2, a) {
    googleMap.setCenter(new GLatLng(l1, l2), a);

    if (marker) {
        googleMap.removeOverlay(marker);
    }

    center = new GLatLng(l1, l2);
    marker = new GMarker(center, { draggable:true });
    GEvent.addListener(marker, 'dragstart', function(){
        googleMap.closeInfoWindow();
    });
    GEvent.addListener(marker, 'dragend', function() {
        var cor = marker.getLatLng();
        updateCoordinate(cor.y, cor.x);
    });

    googleMap.addOverlay(marker);
} // locateMap

function updateCoordinate (l1, l2) {
    $('LocationLatitude').value  = l1;
    $('LocationLongitude').value = l2;
} // updateCoordinate

function addNewState () {
    var state = '<?php __('State/County')?>';
    var area  = '<?php __('Area Name')?>';

    if ($('LocationParentId').selectedIndex == 0) {
        $('LocationParentId').previous('label').update('');
        $('LocationName').previous('label').update(state);
    } else {
        $('LocationParentId').previous('label').update(state);
        $('LocationName').previous('label').update(area);
    }
} // addNewState

addNewState();
<?php
   $location = $form->data['Location'];
   $pr = $location['parent_id'];
   $l1 = $location['latitude'];
   $l2 = $location['longitude'];

   if ($l1 && $l2) {
       $accurate = ($pr) ? 10 : 5;
       echo 'locateMap('.$l1.', '.$l2.', '.$accurate.');';
   }
?>

</script>
